# -*- coding: utf-8 -*-
# @Time    : 2024/1/4 21:39
# @Author  : micah
# @File    : 15.请求重试.py
# @Software: PyCharm


# import time
# from retrying import retry
#
# num = 1
#
#
# @retry(stop_max_attempt_number=3)
# def test():
#     global num
#     print(f'num={num}')
#     num += 1
#     time.sleep(1)
#     raise StopIteration
#
#
# try:
#     test()
# except Exception as e:
#     print('程序异常:', e)


import requests
from retrying import retry

num = 1


@retry(stop_max_attempt_number=3)
def parse_url(url):
    global num
    print(f'num={num}')
    num += 1

    headers = {
        "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1"
    }
    requests.get(url, headers=headers, timeout=3)


try:
    url = 'http://www.google.com'
    parse_url(url)
except Exception as e:
    print('程序异常:', e)
